﻿package mortal.game.view.skill
{
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.guide.navbar.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.skill.panel.*;
    import mortal.game.view.skill.panel.render.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class SkillLearnModule extends BaseWindow
    {
        private var _tabBar:GTabarNew;
        private var _tabBarData:Array;
        private var _careerPanel:SkillCareerPanel;
        private var _basicPanel:SkillBasicPanel;
        private var _equipPanel:SkillEquipPanel;
        private var _curPanel:ISkillSubPanel;
        private var _hideTimerId:int;
        public static const Index_Career:int = 0;
        public static const Index_Basic:int = 1;
        public static const Index_Equip:int = 2;

        public function SkillLearnModule(param1:ILayer = null)
        {
            this._tabBarData = [{label:Language.getString(21094), name:"0"}, {label:Language.getString(21095), name:"1"}, {label:Language.getString(21096), name:"2"}];
            super(param1);
            setSize(892, 538);
            this.title = Language.getString(21097);
            this.titleHeight = 60;
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            if (SkillHintIcon.isShowing())
            {
                SkillHintIcon.instance.hide();
            }
            NavbarButtonUIRow.instance.showHideFilter(ModuleType.Skills, false);
            return;
        }// end function

        public function getItemByPos(param1:int) : SkillItem
        {
            return this._curPanel.getItemByPos(param1);
        }// end function

        public function showSkillEquipPanel(param1:Array) : void
        {
            if (this._tabBar.selectedIndex != Index_Equip)
            {
                this._tabBar.selectedIndex = Index_Equip;
            }
            if (this._equipPanel == null)
            {
                this._equipPanel = UICompomentPool.getUICompoment(SkillEquipPanel) as SkillEquipPanel;
                this._equipPanel.x = 7;
                this._equipPanel.y = 64;
            }
            if (this._curPanel != null && this._curPanel != this._equipPanel)
            {
                DisplayUtil.removeMe(this._curPanel as DisplayObject);
            }
            this._curPanel = this._equipPanel;
            if (this._equipPanel.parent == null)
            {
                this.addChild(this._equipPanel);
            }
            this._equipPanel.allSkillInfos = param1;
            if (GuideController.currentGuideItemWin == ModuleType.Skills && Cache.instance.skill.getLearnableOrUpgradableCount() <= 0)
            {
                this.disposeTimer();
                this._hideTimerId = setTimeout(this.timeroutToHide, 600);
            }
            return;
        }// end function

        public function showSkillCareerPanel(param1:Array) : void
        {
            if (this._tabBar.selectedIndex != Index_Career)
            {
                this._tabBar.selectedIndex = Index_Career;
            }
            if (this._careerPanel == null)
            {
                this._careerPanel = UICompomentPool.getUICompoment(SkillCareerPanel) as SkillCareerPanel;
                this._careerPanel.x = 7;
                this._careerPanel.y = 64;
            }
            if (this._curPanel != null && this._curPanel != this._careerPanel)
            {
                DisplayUtil.removeMe(this._curPanel as DisplayObject);
            }
            this._curPanel = this._careerPanel;
            if (this._careerPanel.parent == null)
            {
                this.addChild(this._careerPanel);
            }
            this._careerPanel.allSkillInfos = param1;
            if (GuideController.currentGuideItemWin == ModuleType.Skills && Cache.instance.skill.getLearnableOrUpgradableCount() <= 0)
            {
                this.disposeTimer();
                this._hideTimerId = setTimeout(this.timeroutToHide, 600);
            }
            return;
        }// end function

        private function timeroutToHide() : void
        {
            this._hideTimerId = -1;
            this.hide();
            GuideController.currentGuideItemWin = null;
            return;
        }// end function

        private function disposeTimer() : void
        {
            if (this._hideTimerId > 0)
            {
                clearTimeout(this._hideTimerId);
                this._hideTimerId = -1;
            }
            return;
        }// end function

        public function refreshRedPoint() : void
        {
            if (this._curPanel == null && this._curPanel == this._careerPanel)
            {
                this._careerPanel.refreshRedPoint();
            }
            if (this._equipPanel != null && this._curPanel == this._equipPanel)
            {
                this._equipPanel.refreshRedPoint();
            }
            return;
        }// end function

        public function selectIndex(param1:int) : void
        {
            this._curPanel.selectIndex(param1);
            return;
        }// end function

        public function showSkillBasicPanel(param1:Array) : void
        {
            if (this._tabBar.selectedIndex != Index_Basic)
            {
                this._tabBar.selectedIndex = Index_Basic;
            }
            if (this._basicPanel == null)
            {
                this._basicPanel = UICompomentPool.getUICompoment(SkillBasicPanel) as SkillBasicPanel;
                this._basicPanel.x = 7;
                this._basicPanel.y = 64;
            }
            if (this._curPanel != null && this._curPanel != this._basicPanel)
            {
                DisplayUtil.removeMe(this._curPanel as DisplayObject);
            }
            this._curPanel = this._basicPanel;
            if (this._basicPanel.parent == null)
            {
                this.addChild(this._basicPanel);
            }
            this._basicPanel.allSkillInfos = param1;
            return;
        }// end function

        public function updateSkillDesc(param1:SkillInfo) : void
        {
            if (this._careerPanel != null)
            {
                this._careerPanel.updateSkillInfo(param1);
            }
            if (this._equipPanel != null)
            {
                this._equipPanel.updateSkillInfo(param1);
            }
            return;
        }// end function

        public function updateHaveSkillPoint(param1:int) : void
        {
            if (this._careerPanel != null)
            {
                this._careerPanel.updateHaveSkillPoint(param1);
            }
            if (this._equipPanel != null)
            {
                this._equipPanel.updateHaveSkillPoint(param1);
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._tabBar = UIFactory.gTabBarNew(2, 34, this._tabBarData, 888, 546, 65, 26, this, this.onTabBarSelectedChange, "TabButtonNew");
            LoaderHelp.addResCallBack(ResFileConst.skillPanel, this.onGetRes);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this.disposeTimer();
            if (this._careerPanel != null)
            {
                this._careerPanel.dispose(param1);
                this._careerPanel = null;
            }
            if (this._equipPanel != null)
            {
                this._equipPanel.dispose(param1);
                this._equipPanel = null;
            }
            this._tabBar.dispose(param1);
            return;
        }// end function

        protected function onTabBarSelectedChange(event:Event = null) : void
        {
            if (this._tabBar.selectedIndex == Index_Equip && Cache.instance.role.entityInfo.level < 50)
            {
                if (this._curPanel == this._careerPanel)
                {
                    this._tabBar.selectedIndex = Index_Career;
                }
                else
                {
                    this._tabBar.selectedIndex = Index_Basic;
                }
                MsgManager.showRollTipsMsg(Language.getString(21098));
                return;
            }
            if (this._careerPanel != null)
            {
                this._careerPanel.resetPage();
            }
            if (this._equipPanel != null)
            {
                this._equipPanel.resetPage();
            }
            Global.instance.callLater(this.dispatchChangeIndexEvent);
            return;
        }// end function

        private function dispatchChangeIndexEvent() : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.SkillPanel_ChangedToIndex, this._tabBar.selectedIndex));
            return;
        }// end function

        private function onGetRes() : void
        {
            if (isDisposed)
            {
                return;
            }
            this._tabBar.selectedIndex = 0;
            this.onTabBarSelectedChange();
            return;
        }// end function

        public function get curIndex() : int
        {
            return this._tabBar.selectedIndex;
        }// end function

    }
}
